package com.bdqn.t330.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.t330.common.DtoUtil;
import com.bdqn.t330.common.ResultStateEnum;
import com.bdqn.t330.entity.Dto;
import com.bdqn.t330.entity.Right;
import com.bdqn.t330.entity.Role;
import com.bdqn.t330.entity.User;
import com.bdqn.t330.service.RoleService;
import com.bdqn.t330.service.UserService;
import io.swagger.annotations.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Api(tags = {"用户接口"})
@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    UserService userService;

    @Resource
    RoleService roleService;

    @RequestMapping("/hello")
    @ResponseBody
    @CrossOrigin
    public String hello(Model model) {
        String msg = "hello:" + "zhangsan";
        model.addAttribute("msg", msg);
        return "hello";
    }

    @RequestMapping("/login")
    public String login() {
        return "login";
    }


    @RequestMapping("/findByUserName")
    @ResponseBody
    @CrossOrigin
    public Object findByUserName(String username, HttpServletRequest request) {
        System.out.println("get");
        String token = request.getHeader("token");
        System.out.println("token:" + token);
        return userService.findByUsrNameBiz(username);
    }

    @PostMapping("/findByUserNamepost")
    @ResponseBody
    @CrossOrigin
    public Object findByUserNamepost(@RequestBody Map map, HttpServletRequest request) {
        System.out.println("post");
        String username = (String) map.get("username");
        String token = request.getHeader("token");
        System.out.println("token:" + token);
        return userService.findByUsrNameBiz(username);
    }

    @ApiOperation(value = "查询用户列表", notes = "查询用户详细列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "当前页", required = false, defaultValue = "1", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", required = false, defaultValue = "5", paramType = "query"),
            @ApiImplicitParam(name = "userName", value = "用户名", required = false, defaultValue = "", paramType = "query"),
            @ApiImplicitParam(name = "userRoleId", value = "角色ID", required = false, defaultValue = "", paramType = "query"),
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header"),

    })
    @GetMapping("/userList")
    @ResponseBody
    @CrossOrigin
    public Dto userList(@RequestParam(value = "pageIndex", required = false, defaultValue = "1") Integer pageIndex,
                        @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize,
                        @RequestParam(value = "userName", required = false, defaultValue = "") String userName,
                        @RequestParam(value = "userRoleId", required = false, defaultValue = "") String userRoleId) {
        Page page = new Page(pageIndex, pageSize);
        QueryWrapper queryWrapper = new QueryWrapper();
        if (userName.trim() != null && !userName.trim().equals("")) {
            queryWrapper.like("usr_name", userName.trim());
        }
        if (userRoleId.trim() != null && !userRoleId.trim().equals("")) {
            queryWrapper.like("usr_role_id", "%" + userRoleId.trim() + "%");
        }
        queryWrapper.orderByAsc("usr_id");
        IPage iPage = userService.selectUserPage(page, queryWrapper);
        return DtoUtil.returnDataSuccess(iPage);
    }

    @RequestMapping("/findrightbyusername")
    @ResponseBody
    @CrossOrigin
    public Object findRightByUserName(@RequestParam(value = "userName", required = true) String userName) {
        List<Right> list = userService.findRightByUsrName(userName);
        return DtoUtil.returnDataSuccess(list);
    }

    @RequestMapping("/add")
    @ResponseBody
    @CrossOrigin
    public Object add(@RequestParam(value = "usrName", required = true) String usrName,
                      @RequestParam(value = "usrPassword", required = false, defaultValue = "") String usrPassword,
                      @RequestParam(value = "roleId", required = true) Long roleId) {
        User user = new User();
        //其它字段添加自己实现
        user.setUsrName(usrName);
        user.setUsrPassword(usrPassword);
        user.setUsrRoleId(roleId);
        boolean bool = false;
        try {
            bool = userService.save(user);
            return DtoUtil.returnSuccess("用户添加", bool);
        } catch (Exception e) {
            return DtoUtil.returnFail("用户添加失败", ResultStateEnum.ERROR.toString());
        }
    }

    @RequestMapping("/deletebyid")
    @ResponseBody
    @CrossOrigin
    public Object deleteById(@RequestParam(value = "id", required = true) Integer id) {
        boolean bool = false;
        try {
            bool = userService.removeById(id);
            return DtoUtil.returnSuccess("用户删除", bool);
        } catch (Exception e) {
            return DtoUtil.returnFail("用户删除失败", ResultStateEnum.ERROR.toString());
        }
    }

    @CrossOrigin
    @ResponseBody
    @RequestMapping(value = "/update")
    public Object update(@RequestParam(value = "id", required = true) Long id,
                         @RequestParam(value = "usrName", required = true) String usrName) {
        User user = new User();
        user.setUsrId(id);
        user.setUsrName(usrName);
        boolean bool = false;
        try {
            bool = userService.updateById(user);
            return DtoUtil.returnSuccess("用户修改", bool);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("用户修改失败", ResultStateEnum.ERROR.toString());
        }
    }

    @RequestMapping("/getrolelist")
    @ResponseBody
    @CrossOrigin
    public Object getRoleList() {
        boolean bool = false;
        List<Role> roleList = roleService.getRoleListBiz();
        return DtoUtil.returnDataSuccess(roleList);
    }
}
